<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }

    </style>
</head>
<body onload="draw();">

</body>
<script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
<script src="js/objects/Lensflare.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/libs/dat.gui.min.js"></script>


<script>
    var renderer;
    function initRender() {

        renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
        renderer.setSize(window.innerWidth, window.innerHeight);
        //告诉渲染器需要阴影效果
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是，没有设置的这个清晰 THREE.PCFShadowMap
        renderer.setClearColor(0x111111);
        renderer.gammaInput = true;
        renderer.gammaOutput = true;
        document.body.appendChild(renderer.domElement);
    }

    var camera;
    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(400, -600, 100);

        camera.lookAt(new THREE.Vector3(-400, 600, -100));
    }

    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    var ambientLight,pointLight;
    function initLight() {
        ambientLight = new THREE.AmbientLight("#111111");
        scene.add(ambientLight);

        pointLight = new THREE.PointLight("#ffffff");
        pointLight.position.set(-400, 600, -100);

        //告诉平行光需要开启阴影投射
        pointLight.castShadow = true;

        scene.add(pointLight);

        // 添加 lens flares
        var textureLoader = new THREE.TextureLoader();

        var textureFlare0 = textureLoader.load("img/lensflare0.png");
        var textureFlare2 = textureLoader.load("img/lensflare2.png");
        var textureFlare3 = textureLoader.load("img/lensflare3.png");

        var flareColor = new THREE.Color(0xffffff);
        flareColor.setHSL(0.55, 0.9, 1.0);

        var lensFlare = new THREE.Lensflare();

        lensFlare.addElement( new THREE.LensflareElement(textureFlare0, 500, 0.0, flareColor) );

        lensFlare.addElement( new THREE.LensflareElement(textureFlare2, 512, 0.0) );
        lensFlare.addElement( new THREE.LensflareElement(textureFlare2, 512, 0.0) );
        lensFlare.addElement( new THREE.LensflareElement(textureFlare2, 512, 0.0) );

        lensFlare.addElement( new THREE.LensflareElement(textureFlare3, 60, 0.6) );
        lensFlare.addElement( new THREE.LensflareElement(textureFlare3, 70, 0.7) );
        lensFlare.addElement( new THREE.LensflareElement(textureFlare3, 120, 0.9) );
        lensFlare.addElement( new THREE.LensflareElement(textureFlare3, 70, 1.0) );

        lensFlare.position.copy(pointLight.position);

        scene.add(lensFlare);
    }


    var cube,plane;
    function initModel() {

        //辅助工具
        var helper = new THREE.AxisHelper(10);
        scene.add(helper);

        //球体
        var sphereGeometry = new THREE.SphereGeometry(10,30,30);
        var sphereMaterial = new THREE.MeshStandardMaterial({color:0xff00ff});
        var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
        sphere.position.set(-20,20,0);

        sphere.castShadow = true;

        scene.add(sphere);

        //立方体
        var cubeGeometry = new THREE.CubeGeometry(10,10,10);

        var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff});

        cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.position.x = 30;
        cube.position.y = 5;
        cube.position.z = -5;

        //告诉立方体需要投射阴影
        cube.castShadow = true;

        scene.add(cube);

        //底部平面
        var planeGeometry = new THREE.PlaneGeometry(5000, 5000, 20, 20);
        var planeMaterial = new THREE.MeshStandardMaterial({color: 0xaaaaaa});

        plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.y = -0;

        //告诉底部平面需要接收阴影
        plane.receiveShadow = true;

        scene.add(plane);

    }

    //初始化性能插件
    var stats;
    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    //用户交互插件 鼠标左键按住旋转，右键按住平移，滚轮缩放
    var controls,clock;
    function initControls() {

        controls = new THREE.OrbitControls( camera, renderer.domElement );

        // 如果使用animate方法时，将此函数删除
        //controls.addEventListener( 'change', render );
        // 使动画循环使用时阻尼或自转 意思是否有惯性
        controls.enableDamping = true;
        //动态阻尼系数 就是鼠标拖拽旋转灵敏度
        //controls.dampingFactor = 0.25;
        //是否可以缩放
        controls.enableZoom = true;
        //是否自动旋转
        controls.autoRotate = true;
        controls.autoRotateSpeed = 0.5;
        //设置相机距离原点的最远距离
        controls.minDistance  = 1;
        //设置相机距离原点的最远距离
        controls.maxDistance  = 200;
        //是否开启右键拖拽
        controls.enablePan = true;
    }


    //初始化dat.GUI简化试验流程
    var param;
    function initGui() {

    }

    function render() {
        renderer.render(scene, camera);

        //更新性能插件
        stats.update();

        controls.update();
    }

    //窗口变动触发的函数
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        render();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    function animate() {

        //更新控制器
        render();

        requestAnimationFrame(animate);
    }

    function draw() {
        initRender();
        initScene();
        initCamera();
        initLight();
        initModel();
        initControls();
        initStats();
        initGui();

        animate();
        window.onresize = onWindowResize;
    }
</script>
</html>